package com.huike.clues.service.impl;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.dto.TbClueTrackDTO;
import com.huike.clues.domain.vo.ClueTrackVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackMapper;
import com.huike.clues.service.ITbClueTrackService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TbClueTrackServiceImpl implements ITbClueTrackService {
    @Autowired
    private TbClueMapper tbClueMapper;
    @Autowired
    private TbClueTrackMapper tbClueTrackMapper;

    @Override
    public void addRecord(ClueTrackVo clueTrackVo) {
        //获取用户账户
        String username = SecurityUtils.getUsername();

        TbClue tbClue = new TbClue();
        tbClue.setId(clueTrackVo.getClueId());
        tbClue.setName(clueTrackVo.getName());
        tbClue.setSex(clueTrackVo.getSex());
        tbClue.setWeixin(clueTrackVo.getWeixin());
        tbClue.setQq(clueTrackVo.getQq());
        tbClue.setAge(clueTrackVo.getAge());
        tbClue.setNextTime(clueTrackVo.getNextTime());
        tbClue.setUpdateTime(new Date());
        tbClue.setUpdateBy(username);
        int i = tbClueMapper.updateTbClue(tbClue);
        System.out.println(i);

        //线索跟进
        clueTrackVo.setCreateBy(username);
        clueTrackVo.setCreateTime(new Date());
        Integer integer = tbClueTrackMapper.insertClueTrack(clueTrackVo);
        System.out.println(integer);
    }



    @Override
    public void addFalseReason(long clueId, Map<String, Object> map) {
        TbClue tbClue = tbClueMapper.selectTbClueById(clueId);
        int falseCount = tbClue.getFalseCount();
        if (falseCount<2){
            tbClue.setFalseCount(falseCount+1);
            tbClueMapper.updateTbClue(tbClue);
        }else {
            tbClueMapper.deleteTbClueById(clueId);
        }

        String username = SecurityUtils.getUsername();
        ClueTrackVo clueTrackVo = new ClueTrackVo();
        clueTrackVo.setClueId(clueId);
        clueTrackVo.setCreateBy(username);
        clueTrackVo.setCreateTime(new Date());
        clueTrackVo.setFalseReason(Integer.parseInt((String) map.get("reason")));
        clueTrackVo.setRemark((String) map.get("remark"));
        clueTrackVo.setType("1");
        tbClueTrackMapper.insertClueTrack(clueTrackVo);
    }

    @Override
    public List<TbClueTrackDTO> selectList(Long clueId) {
        return tbClueTrackMapper.selectList(clueId);
    }
}
